package com.situ.ymall.controller;

import com.situ.ymall.entity.Shopcart;
import com.situ.ymall.entity.User;
import com.situ.ymall.service.ShopCartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;

@Controller
@RequestMapping("/user/cart")
public class CartController {

    @Autowired
    private ShopCartService shopCartService;
    /**
     * 加入购物车
     */
    @GetMapping("/add")
    public String add(Integer id, HttpSession session){
        User user = (User) session.getAttribute("user");

        Shopcart shopcart = new Shopcart();
        shopcart.setUserId(user.getId());
        shopcart.setGoodsId(id);
        shopcart.setCount(1);
        //调用controller
        shopCartService.add(shopcart);

        return "redirect:/user/cart";
    }

    @GetMapping
    public String cart(){
        return "user/cart";
    }

}
